//
//  AddShopperViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/13.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class AddShopperViewController: BaseViewController,UITextFieldDelegate {

    var field : UITextField = UITextField.init()
    weak var delegate : ShopperListViewController?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.title = "添加营业员"
        self.defaultBackButton()
        self.rightButton(title: "保存", target: self, selector: #selector(save))
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "f3f3f5")
        field.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 30 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: 88 * YXHeightScale))
        field.layer.borderColor = UIColor.colorWithHexString(hex: "d9d9d9").cgColor
        field.clearButtonMode = .whileEditing
        field.rightViewMode = .always
        field.textColor = UIColor.black
        field.backgroundColor = UIColor.white
        field.delegate = self
        field.font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        self.view.addSubview(field)
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.text!.count > 8 {
            if string == "" {
                return true
            }
            return false
        }
        return true
    }
    
    @objc func save() -> Void {
        let result = NumberChecker.validName(name: field.text)
        if result == true {
            self.beginAnim()
            Manager.shared().addShopper(name: field.text!, success: {(model) in
                self.endAnim()
                if model.result == true {
                    //更新前一个控制器
                    self.delegate?.reload()
                    self.alertSomeMessage(title: YXAlertTitle, content: model.msg, sureTitle: YXConfirmTitle, cancelTitle: nil, ensureAction: { (alert) in
                        let _ = self.navigationController?.popViewController(animated: true)
                    }, cancelAction: { (alert) in
                        
                    })
                }else{
                    self.commonAlertMsg(content: model.msg)
                }
            }, fail: {(code) in
                self.endAnim()
                self.commonAlertMsg(content: YXNetWorkFail)
            })
        }else{
            commonAlertMsg(content: "请输入合法的名称!")
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
